//
//  MeController.swift
//  chat
//
//  Created by 崔世强 on 2018/11/5.
//  Copyright © 2018 崔世强. All rights reserved.
//

import UIKit
import Kingfisher
import SwiftyUserDefaults

class MeController: UITableViewController {
    @IBOutlet weak var avatar: UIImageView!
    
    @IBOutlet weak var nickNameLabel: UILabel!
    
    @IBOutlet weak var uidLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        let Defaults = UserDefaults(suiteName: USER_DEFAULTS_NAME)!
        nickNameLabel.text = Defaults[.username]
        uidLabel.text = String(Defaults[.userId] ?? 0)
        let avatar = Defaults[.userAvatar] ?? ""
        self.avatar.kf.setImage(with: KF.ImageResource(downloadURL: Common.imgOrigUrl(avatar) ?? URL(string: "localhost")!), placeholder: #imageLiteral(resourceName:"defaultAvatar"),options: [.processor(CryptorProcessor())])
    }

    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
//    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//        // Get the new view controller using segue.destination.
//        // Pass the selected object to the new view controller.
//
//
//    }
 
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if (indexPath.section,indexPath.row) == (2,0) {
            userQuit()
        }
    }
    
    func userQuit(){
//        let deviceToken = UserDefaults.standard.string(forKey: "deviceToken")
//        UIPasteboard.general.string = deviceToken
//
        let quitAction = UIAlertAction(title: "退出登录", style: .destructive) { (action) in
            let Defaults = UserDefaults(suiteName: "group.missle-chat")!
            Defaults[.userId] = nil
            Defaults[.username] = nil
            EventProcessorWrapper().process(eventName: "logout", params: [:])
            let userController = UserViewController()
            UIApplication.shared.keyWindow?.rootViewController = userController
        }
        let cancelAction = UIAlertAction(title: "取消", style: .cancel)
        self.showActionSheet(title: nil, message: "退出后不会删除任何历史数据，下次登录依然可以使用本账号", actions: [quitAction,cancelAction], animated: true)
    }
}
